package com.yiren.backstage.communication.analysis;

import android.content.Context;

import com.yiren.backstage.communication.interfaces.DataCallback;
import com.yiren.backstage.communication.utils.ZiyanGyroGearUtil;
import com.yiren.backstage.currency.bean.CarStateBean;
import com.yiren.backstage.currency.bean.GyroData;

/**
 * Time:2021/12/03 9:34
 * Author:xiaozan
 * Description:角度数据帧解析
 */
public class GyroDataAnalysis {
    private DataCallback ziyanDataCallback; //自研档位数据回调
    public CarStateBean carState = CarStateBean.getInstance();
    private Context context;
    private  ZiyanGyroGearUtil ziyanGyroGearUtil;
    private String dataGyro;

    private int noUpCount = 0;  //角度值不更新累计次数

    private static GyroDataAnalysis dataAnalysisGYRO = new GyroDataAnalysis();


    public static GyroDataAnalysis getInstance(Context context) {
        return dataAnalysisGYRO;
    }

    public void initCarMasterTelent(Context context) {
        this.context = context;
        ziyanGyroGearUtil = ZiyanGyroGearUtil.getInstance(context);
        ziyanGyroGearUtil.setZiyanGearData();
    }

    public void getZiyanData(DataCallback ziyanDataCallback) {
        this.ziyanDataCallback = ziyanDataCallback;
        dataGyro = GyroData.getLines();
        if (dataGyro.contains("GYRO") && dataGyro.startsWith("$") && dataGyro.endsWith("/>")) {
            if (null != ziyanDataCallback) {
                ziyanDataCallback.onReadData(dataGyro);
            }
        }
    }

    public void dataAnalysis(String data) {
        try {
            // 验证数据
            String[] carDataArray = data.split(";");
            //角度值过滤，如果当前角度值跟上次角度差值大于50度不更新数据
            float nsbsAngleDiff = Math.abs(Float.parseFloat(carDataArray[1]));
            //角度乱跳数据过滤   排查跳动50-250度的跳动     角度范围值是0-360度
            if (noUpCount < 10 && carState.getNsbsAngleDiff() != 0 && Math.abs(nsbsAngleDiff - carState.getNsbsAngleDiff()) > 50 && Math.abs(nsbsAngleDiff - carState.getNsbsAngleDiff()) <= 250) {
                noUpCount++;
            } else {
                carState.setNsbsAngleDiff(nsbsAngleDiff);
                noUpCount = 0;
            }
            carState.setPitchAngle(Float.parseFloat(carDataArray[3]) + Float.parseFloat(carDataArray[5]));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
